import config from '../../../config';
import { Zone } from '../../buildings/zones/Zone';
import { SimModule } from './SimModule.js';

export class Development extends SimModule {

    static state = {
        abandoned: 'abandoned',
        developed: 'developed',
        underConstruction: 'under-construction',
        undeveloped: 'undeveloped',
    };

    /**
     * Number of simulation steps that building has met abandonment criteria
     * If abandonment criteria are not met, value is zero
     */
    private abandonmentCounter = 0;

    /** 施工天数 */
    private constructionCounter = 0;

    /** 发展水平 */
    private _level = 1;

    /** 最大发展水平 */
    private _maxLevel = 3;

    private _state = Development.state.undeveloped;

    private zone: Zone;

    get level() {
        return this._level;
    }

    set level(value) {
        this._level = value;
        this.zone.refreshView();
    }

    get state() {
        return this._state;
    }

    set state(value) {
        this._state = value;
        this.zone.refreshView();
    }

    get maxLevel() {
        return this._maxLevel;
    }

    constructor(zone: Zone) {
        super();
        this.zone = zone;
    }

    simulate() {

        this.checkAbandonmentCriteria();

        let needsRefresh = false;

        switch (this._state) {

            // 建筑状态：未开发
            case Development.state.undeveloped:

                // 检查发展标准
                if (this.checkDevelopmentCriteria() &&
                    Math.random() < config.modules.development.redevelopChance) {
                    this._state = Development.state.underConstruction;
                    this.constructionCounter = 0;
                    needsRefresh = true;
                }
                break;

            // 建筑状态：正在建设
            case Development.state.underConstruction:
                this.constructionCounter++;
                if (this.constructionCounter === config.modules.development.constructionTime) {
                    this._state = Development.state.developed;
                    this._level = 1;
                    this.constructionCounter = 0;
                    needsRefresh = true;
                }
                break;

            // 建筑状态：发展中
            case Development.state.developed:
                if (this.abandonmentCounter > config.modules.development.abandonThreshold) {
                    if (Math.random() < config.modules.development.abandonChance) {
                        this._state = Development.state.abandoned;
                        needsRefresh = true;
                    }
                } else {
                    if (this._level < this._maxLevel && Math.random() < config.modules.development.levelUpChance) {
                        this._level++;
                        needsRefresh = true;
                    }
                }
                break;

            // 建筑状态：废弃
            case Development.state.abandoned:
                if (this.abandonmentCounter == 0) {
                    if (Math.random() < config.modules.development.redevelopChance) {
                        this._state = Development.state.developed;
                        needsRefresh = true;
                    }
                }
                break;
        }

        if (needsRefresh === true) {
            this.zone.refreshView();
        }
    }

    /** 检查城市发展标准 */
    private checkDevelopmentCriteria() {
        return this.zone.roadAccess.value && this.zone.power.isFull;
    }

    private checkAbandonmentCriteria() {
        if (!this.checkDevelopmentCriteria()) {
            this.abandonmentCounter++;
        } else {
            this.abandonmentCounter = 0;
        }
    }

    toHTML() {
        return `
        <span class="info-label">State </span>
        <span class="info-value">${this._state}</span>
        <br>
        <span class="info-label">Level </span>
        <span class="info-value">${this._level}</span>
        <br>`;
    }
}